package com.example.bbs.controller;

import com.example.bbs.bean.Admin;
import com.example.bbs.bean.Bbstag;
import com.example.bbs.bean.Bbstype;
import com.example.bbs.bean.Note;
import com.example.bbs.http.bean.HttpSendBasic;
import com.example.bbs.http.bean.admin.AdminStatue;
import com.example.bbs.http.bean.file.UploadFile;
import com.example.bbs.http.bean.type.HomePageData;
import com.example.bbs.service.AdminService;
import com.example.bbs.service.NoteService;
import com.example.bbs.service.TypeService;
import com.sun.org.apache.xpath.internal.functions.FuncSubstring;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/api/admin")
@Api(tags = {"管理员接口"})
//@ResponseBody
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private NoteService noteService;

    /**
     * 管理员页面
     */
    @RequestMapping("/")
    public String manageCategory(Model model){
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return  "manageCategory";
    }

    /**
     * 管理员-帖子
     */
    @RequestMapping("/manageNote")
    public String manageNote(Model model){
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        List<Note> notes = noteService.getNoteList();
        model.addAttribute("notes", notes);
        return "manageNote";
    }



    /**
     * 管理员-登陆
     */
    @PostMapping("/login")
    @ApiOperation("管理员登录")
    public String login(@RequestParam("name") String name, @RequestParam("pass") String pass, HttpServletRequest request,
                               Model model) {
        Admin admin = adminService.login(name, pass);
        HttpSession session = request.getSession();
        HttpSendBasic sendBasic = new HttpSendBasic();
        if (admin == null) {
            sendBasic.setSuccess(0);
            sendBasic.setMessage("错误的账号或密码");
            model.addAttribute("message", sendBasic.getMessage());
            return "adminLogin";
        }
        session.setAttribute("admin", admin);
        sendBasic.setSuccess(1);
        sendBasic.setMessage("ok");
        HomePageData homePageData = typeService.homePageInit();
        model.addAttribute("types", homePageData);
        return "manageCategory";
    }

    @GetMapping("/statue")
    @ApiOperation("管理员登录状态")
    public HttpSendBasic statue(HttpServletRequest request) {
        AdminStatue sendBasic = new AdminStatue();
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") == null) {
            sendBasic.setSuccess(0);
            sendBasic.setMessage("未登录");
            return sendBasic;
        }
        sendBasic.setSuccess(1);
        sendBasic.setAdminname(((Admin) session.getAttribute("admin")).getAdminname());
        sendBasic.setMessage("ok");
        return sendBasic;
    }

    @PostMapping("/file")
    @ApiOperation("管理员上传文件")
    @ResponseBody
    public UploadFile uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            String s = adminService.uploadFile(file);
            UploadFile uploadFile = new UploadFile();
            uploadFile.setMessage("ok");
            uploadFile.setUrl(s);
            uploadFile.setSuccess(1);
            return uploadFile;
        } catch (IOException e) {
            e.printStackTrace();
            UploadFile uploadFile = new UploadFile();
            uploadFile.setMessage("IO错误");
            uploadFile.setSuccess(0);
            return uploadFile;
        }
    }

    @PostMapping("/note/edit")
    @ApiOperation("管理员修改帖子")
    @ResponseBody
    public HttpSendBasic admineditNote(@RequestParam("noteId") Integer noteId, @RequestParam(value = "typeid",required = false) Integer typeid,
                                       @RequestParam(value = "noteName",required = false) String noteName, @RequestParam(value = "note",required = false) String note) {
        return adminService.editNote(noteId, typeid, noteName, note);
    }

    @PostMapping("/note/")
    @ApiOperation("管理员修改帖子")
    @ResponseBody
    public HttpSendBasic note(@RequestParam("noteId") Integer noteId) {
        return noteService.getNote(noteId);
    }


    @PostMapping("/note/delete")
    @ApiOperation("管理员删除帖子")
    @ResponseBody
    public HttpSendBasic admindeleteNote(@RequestParam("noteId") Integer noteId) {
        return adminService.deleteNote(noteId);
    }

    @PostMapping("/note/setTop")
    @ApiOperation("管理员设置置顶帖")
    @ResponseBody
    public HttpSendBasic adminSetNoteTop(@RequestParam("noteId") Integer noteId,@RequestParam("top") Integer top){
        return adminService.setNoteTop(top, noteId);
    }

    @PostMapping("/note/setGood")
    @ApiOperation("管理员设置精华帖")
    @ResponseBody
    public HttpSendBasic adminSetNoteGood(@RequestParam("noteId") Integer noteId,@RequestParam("isGood") Integer isGood){
        return adminService.setTypeGood(isGood, noteId);
    }

    @PostMapping("/note/setLight")
    @ApiOperation("管理员设置加亮")
    @ResponseBody
    public HttpSendBasic adminSetNoteLight(@RequestParam("noteId") Integer noteId,@RequestParam("tittleColor") String tittleColor){
        return adminService.setTypeTittlecolor(tittleColor, noteId);
    }


    @PostMapping("/comment/edit")
    @ApiOperation("管理员修改评论")
    @ResponseBody
    public HttpSendBasic admineditComment(@RequestParam("commentId") Integer commentId,@RequestParam("text") String text){
        return adminService.editComment(commentId, text);
    }

    @PostMapping("/comment/delete")
    @ApiOperation("管理员删除评论")
    @ResponseBody
    public HttpSendBasic adminDeleteComment(@RequestParam("commentId") Integer commentId){
        return adminService.deleteComment(commentId);
    }

    @PostMapping("/tag/add")
    @ApiOperation("管理员添加板块")
    @ResponseBody
    public HttpSendBasic adminAddTagType(@RequestParam("tagName") String tagName){
        return adminService.addTagType(tagName);

    }

    @PostMapping("/tag/update")
    @ApiOperation("管理员修改板块")
    @ResponseBody
    public HttpSendBasic adminUpdateTagType(@RequestParam("tagId") Integer tagId,@RequestParam("tagName") String tagName){
        return adminService.updateTagType(tagId,tagName);
    }

    @PostMapping("/tag/delete")
    @ApiOperation("管理员删除板块")
    @ResponseBody
    public HttpSendBasic adminDelteTagType(@RequestParam("tagId") Integer tagId){
        return adminService.deleteTagType(tagId);
    }

    @PostMapping("/type/add")
    @ApiOperation("管理员添加分类")
    @ResponseBody
    public HttpSendBasic adminAddType(@RequestParam("tagId") Integer tagId,@RequestParam("typeName") String typeName,
                                      @RequestParam(name = "icon",required = false) String icon){
        return adminService.addType(tagId, typeName, icon);
    }

    @PostMapping("/type/update")
    @ApiOperation("管理员修改分类")
    @ResponseBody
    public HttpSendBasic adminUpdateType(@RequestParam("tagId") Integer tagId,@RequestParam("typeName") String typeName,
                                          @RequestParam(name = "icon",required = false) String icon,@RequestParam("typeId") Integer typeId){
        return adminService.updateType(typeId, tagId, typeName, icon);
    }

    @PostMapping("/type/delete")
    @ApiOperation("管理员删除分类")
    @ResponseBody
    public HttpSendBasic adminDeleteType(@RequestParam("typeId") Integer typeId){
        return adminService.deleteType(typeId);
    }

    @PostMapping("/note/getType")
    @ResponseBody
    //通过tagid获取分类
    public List<Bbstype> getType(@RequestParam("tagId") Integer tagId){
        List<Bbstype> types = noteService.selectTypeByTagid(tagId);
        return types;
    }

    @GetMapping("/manageComment")
    public String getComments(@RequestParam("noteid")Integer noteid,Model model){
        model.addAttribute("comments",noteService.commentList(noteid));
        return "manageComment";
    }



}
